package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        //将图片保存到Redis
        saveRedis(setmeal.getImg());
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }
    //将图片名称保存到Redis
    private void saveRedis(String img) { jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES,img); }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * @return
     * C端的体检预约查询列表_查询所有
     */
    @Override
    public List<Setmeal> findAll() {
        return setMealDao.findAll();
    }

    /**
     * 根据套餐id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        // 根据id获取套餐数据
        Setmeal setmeal = setMealDao.findById(id);
        // 根据表获取关联表中的数据id
        Integer[] ids = setMealDao.selectByMG(id);
        // 遍历id,获取group,并放入list中
        List<CheckGroup> list = new ArrayList<>();
        for (Integer integer : ids) {
            CheckGroup checkGroup = checkGroupDao.selectById(integer);
            // 根据id查询关联表信息
            List<Integer> items = checkGroupDao.selectCheckItemIdsByCheckGroupId(integer);
            List<CheckItem> itemList = new ArrayList<>();
            for (Integer item : items) {
                // 根据id查询Item,放入group
                CheckItem checkItem = checkItemDao.selectById(item);
                itemList.add(checkItem);
            }
            checkGroup.setCheckItems(itemList);
            list.add(checkGroup);
        }
        setmeal.setCheckGroups(list);
        return setmeal;
    }
}
